package com.orderplus.opshop.core.page;

import java.io.Serializable;

public class QueryParameter implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final String ASC = "asc";
	public static final String DESC = "desc";
	protected int pageNo = 1;

	protected int pageSize = 0;

	protected String orderBy = null;

	protected String order = "asc";

	protected boolean autoCount = true;

	public QueryParameter() {
	}

	public QueryParameter(int pageNo, int pageSize) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public boolean isPageSizeSetted() {
		return this.pageSize > -1;
	}

	public int getPageNo() {
		return this.pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getFirst() {
		if ((this.pageNo < 1) || (this.pageSize < 1)) {
			return -1;
		}
		return (this.pageNo - 1) * this.pageSize;
	}

	public boolean isFirstSetted() {
		return (this.pageNo > 0) && (this.pageSize > 0);
	}

	public String getOrderBy() {
		return this.orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public boolean isOrderBySetted() {
		return (this.orderBy != null) && (!this.orderBy.trim().equals(""));
	}

	public String getOrder() {
		return this.order;
	}

	public void setOrder(String order) {
		if (("asc".equalsIgnoreCase(order)) || ("desc".equalsIgnoreCase(order))) {
			this.order = order.toLowerCase();
		} else {
			throw new IllegalArgumentException("order should be 'desc' or 'asc'");
		}
	}

	public boolean isAutoCount() {
		return this.autoCount;
	}

	public void setAutoCount(boolean autoCount) {
		this.autoCount = autoCount;
	}

}
